TEXTURE2D (_MetallicSpecGlossMap);
SAMPLER (sampler_MetallicSpecGlossMap);
TEXTURE2D (_OcclusionMap);
SAMPLER (sampler_OcclusionMap);

half SampleOcclusion(float2 uv)
{
    #ifdef _OCCLUSIONMAP
    // TODO: Controls things like these by exposing SHADER_QUALITY levels (low, medium, high)
    #if defined(SHADER_API_GLES)
    return SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
    #else
    half occ = SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
    return LerpWhiteTo(occ, _OcclusionStrength);
    #endif
    #else
    return half(1.0);
    #endif
}

/*028没有用了
//unity的源码实现的
half4 SampleSpecularSmoothness(float2 uv, half alpha, half4 specColor, TEXTURE2D_PARAM (specMap, sampler_specMap))
{
    half4 specularSmoothness = half4(0, 0, 0, 1);
    #ifdef _SPECGLOSSMAP
    specularSmoothness = SAMPLE_TEXTURE2D(specMap, sampler_specMap, uv) * specColor;
    #elif defined(_SPECULAR_COLOR)
    specularSmoothness = specColor;
    #endif

    #ifdef _GLOSSINESS_FROM_BASE_ALPHA
    specularSmoothness.a = alpha;
    #endif

    return specularSmoothness;
}
*/

//unity源码实现的————根据老师说的改
/*
#ifdef _SPECULAR_SETUP
#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_SpecGlossMap, sampler_SpecGlossMap, uv)
#else
#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicGlossMap, sampler_MetallicGlossMap, uv)
#endif
*/
#ifdef _SPECULAR_SETUP
#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicSpecGlossMap, sampler_MetallicSpecGlossMap, uv)
#else
#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicSpecGlossMap, sampler_MetallicSpecGlossMap, uv)
#endif

//unity源码实现的
half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
{

    half4 specGloss;

    #ifdef _METALLICSPECGLOSSMAP
    specGloss = half4(SAMPLE_METALLICSPECULAR(uv));
    #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    specGloss.a = albedoAlpha * _Smoothness;
    #else
    specGloss.a *= _Smoothness;
    #endif
    #else // _METALLICSPECGLOSSMAP
    #if _SPECULAR_SETUP
    specGloss.rgb = _SpecColor.rgb;
    #else
    specGloss.rgb = _Metallic.rrr;
    #endif

    #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    specGloss.a = albedoAlpha * _Smoothness;
    #else
    specGloss.a = _Smoothness;
    #endif
    #endif

    return specGloss;
}

/*
struct SurfaceData
{
    half3 albedo;
    half3 specular;
    half  metallic;
    half  smoothness;
    half3 normalTS;
    half3 emission;
    half  occlusion;
    half  alpha;
    half  clearCoatMask;
    half  clearCoatSmoothness;
};
             */
void InitSurfaceData(Varyings IN, out SurfaceData surfaceData)
{
    surfaceData = (SurfaceData)0;
    //unity已经封装好了采样basemap 的 albedo颜色 /采样basemap透明度 做alpha clip 组合的功能
    /*028new
    half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
    #ifdef _ALPHATEST_ON
        clip(baseMap.a - _Cutoff);
    #endif

    half4 diffuse = baseMap * _BaseColor * IN.color;
    */
    half4 albedoAlpha = SampleAlbedoAlpha(IN.uv,_BaseMap,sampler_BaseMap);
    //专门读取a通道
    surfaceData.alpha = Alpha(albedoAlpha.a,_BaseColor,_Cutoff);
    
    //028new
    //surfaceData.albedo = diffuse.rgb;
    surfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb *IN.color.rgb;
    surfaceData.normalTS = SampleNormal(IN.uv, _BumpMap, sampler_BumpMap);
    surfaceData.emission = SampleEmission(IN.uv, _EmissionColor, _EmissionMap, sampler_EmissionMap);
    //surfaceData.occlusion = 1.0;
    surfaceData.occlusion = SampleOcclusion(IN.uv);
    //028之前是只采样高光和平滑度，现在
    //half4 specular = SampleSpecularSmoothness(IN.uv, baseMap.a, _SpecColor, _MetallicSpecGlossMap, sampler_MetallicSpecGlossMap);
    //存储了金属度贴图的信息,
    half4 specGloss = SampleMetallicSpecGloss(IN.uv,albedoAlpha.a);
    #ifdef  _SPECULAR_SETUP
    //高光工作流
        surfaceData.metallic = 1.0h;
        surfaceData.specular = specGloss.rgb;
        //surfaceData.specular = half3(0.0h,0.0h,0.0h);
    #else
    //金属工作流
        //金属度只占一个通道
         surfaceData.metallic = specGloss.r;
         surfaceData.specular = half3(0.0h,0.0h,0.0h);
    #endif
    
    //028
    //surfaceData.specular = specular.rgb;
    //surfaceData.smoothness = specular.a * _Smoothness;
    surfaceData.smoothness = specGloss.a ;
}
